package pl.elka.simulator.gui.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import pl.elka.simulator.core.Position;
import pl.elka.simulator.gui.swt.painting.SimulationPainter;
import pl.elka.simulator.mediator.ActionType;
import pl.elka.simulator.mediator.ActionValue;
import pl.elka.simulator.mediator.SimulationFasade;

public class DrawBoard extends Canvas implements IReloadable{

	public DrawBoard(Composite parent) {
		super(parent, SWT.NULL);
		Color col = new Color(parent.getDisplay(),255,255,255);

        this.setBackground(col);
        col.dispose();
        
        addPaintListener(new SimulationPainter());
        addMouseListener(new MouseAdapter() {
        	public void mouseDoubleClick(MouseEvent e) {
        		Position pos = PositionConverter.convertToPosition(e.x, e.y);
        		ActionValue av = new ActionValue(pos);
        		SimulationFasade.getInstance().handleAction(ActionType.PositionSelected, av);
        		DrawBoard.this.redraw();
        	}
		});
        
	}

	@Override
	public void reloadContent() {
		redraw();
	}

}
